package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.utils.SendMsgUtils;
import jdk.internal.org.objectweb.asm.tree.IincInsnNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @PostMapping
    public R saveCategory(@RequestBody Category category){
        log.info("此时category为：{}",category);
        categoryService.save(category);
        return R.success(null);
    }

    @GetMapping("/page")
    public R pageCategory(int page, int pageSize){
        //1.分页构造器
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //2.给个排序,根据sort进行排序

        lqw.orderByDesc(Category::getSort);
        //3.分页查询
        categoryService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }
    @PutMapping
    public R updateCategory(@RequestBody Category category){
        log.info("获取当前id为：{}",category.getId());
        //根据id进行修改
        categoryService.updateById(category);
        return R.success(null);
    }
    @DeleteMapping
    public R deleteByIds(Long ids){
        log.info("删除传入ids为：{}",ids);
        categoryService.remove(ids);
        return R.success(null);
    }
    @GetMapping("/list")
    public R dishCategory(Category category) {
        /*
            1.设置条件
            2.判断type是否等于null
            3.排序
            4.查询
         */
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getSort);
        if (category.getType() != null) {
            lqw.eq(Category::getType, category.getType());
        }
        List<Category> lists = categoryService.list(lqw);
        return R.success(lists);

    }

}
